<?php  if (!defined('BASEPATH')) exit('No direct script access allowed');

class Teknisi extends Controller{
	
	private $sidebar_menu_links = array(
		'detail' => 'Detail',
		'jadwal_kunjungan' => 'Jadwal Kunjungan',
	);


	function __construct(){
		parent::Controller();
		$this->current_user = models\Current_user::get_user();
		if(!models\Current_user::previlege('Administrator|Agen'))
			show_error(array('Halaman ini tidak bisa diakses oleh anda!',anchor('','Kembali ke halaman utama')),403);
		$this->next_prev_calendar_url = base_url().$this->uri->segment(1)."/ajax_calendar/";
	}
	
	function index(){
	
		$this->load->library('pagination');

		$total_rows = $this->doctrine->em
			->createQuery('SELECT COUNT(t.id) FROM models\Teknisi t')
			->getSingleScalarResult();
		
		$config = array(
			'base_url' =>  base_url().'/'.$this->uri->segment(1).'/index',
			'total_rows' => $total_rows,
			'per_page' => 10,
			'num_links' => 10,
			'full_tag_open' => "<div id='pagination'>",
			'full_tag_close' => "</div>",
		);

		$this->pagination->initialize($config);

		if(!$this->uri->segment(3)) $offset = 0;
		else $offset = $this->uri->segment(3);
		
		$teknisi = $this->doctrine->em
			->createQuery('SELECT t FROM models\Teknisi t ORDER BY t.username ASC')
			->setMaxResults($config['per_page'])
			->setFirstResult($offset)
			->getResult();

		$this->headbar->add_header();
		$this->headbar->add_menu('register','Daftar Teknisi Baru',$this->uri->segment(1).'/register');

		foreach($teknisi as $t){

			$block = new Block();

			$block->add_grid()
				->add_info($t->get_username())
				->add_nama_info($t,'images/orang.png')
				->add_info($t->get_keahlian(),'images/gir.png');

			$block->add_grid()
				->add_info(
					$t->get_alamat().br().
					$t->get_kota().' '.
					$t->get_kodepos().br().
					$t->get_propinsi(),
					'images/rumah.png'
				);

			$block->add_grid()
				->add_info($t->get_telepon(),'images/telepon.png')
				->add_info(mailto($t->get_email()),'images/edit.png');

			$block->set_class('daftar_akun');

			$this->main_content->add_block($block);
		}

		$this->main_content->add_pagination();

		$this->sidebar->create_custom('p','Daftar seluruh teknisi Smart Connect Internet Home Services');
		
        $this->load->view('template', array('title' => 'Teknisi'));
	}
	
	function detail($username = null){
	
		if(!$username) show_404();

		// mendapatkan objek Teknisi berdasarkan username
		$teknisi = $this->doctrine->em->getRepository('models\Teknisi')->findOneBy(array('username' => $username));
		if(!$teknisi) show_404();
		
		$this->main_content->add_custom('p','Detail agen '.$teknisi->get_nama());

		$this->main_content->add_block('Akun')
			->add_hidden_field('id',$teknisi->get_id())
			->add_editable_username($teknisi)
			->add_editable_data('nama','Nama',$teknisi->get_nama());

		$this->main_content->add_block('Keahlian')
			->add_editable_textarea_data('keahlian','Keahlian',$teknisi->get_keahlian());

		$this->main_content->add_block('Alamat')
			->add_editable_data('alamat','Alamat',$teknisi->get_alamat())
			->add_editable_data('kota','Kota',$teknisi->get_kota())
			->add_editable_data('kodepos','Kode Pos',$teknisi->get_kodepos())
			->add_editable_propinsi($teknisi->get_propinsi());

		$this->main_content->add_block('Kontak')
			->add_editable_data('telepon','No Telepon',$teknisi->get_telepon())
			->add_editable_email($teknisi->get_email());

		$this->main_content->set_editable();
		$this->main_content->set_deletable();

		$this->headbar->add_header();
		$this->sidebar
			->create_identitas($teknisi)
			->create_menu($this->sidebar_menu_links);

        $this->load->view('template', array('title' => 'Detail Teknisi'));

    }

	function ajax_edit($username = null){
		if(IS_AJAX){

			$this->load->library('form_validation');

			if($this->form_validation->run('teknisi_update') == TRUE){

				$teknisi = $this->doctrine->em->getRepository('models\Teknisi')->findOneBy(array('username' => $username));
				if(!$teknisi) {
					$returned_data = array('status' => 'gagal');
					echo json_encode($returned_data);
					return false;
				}

				$username = $this->generator->generate_username('models\Teknisi',$this->input->post('username'));

				$teknisi->set_username($username);
				$teknisi->set_nama($this->input->post('nama'));

				$teknisi->set_keahlian($this->input->post('keahlian'));

				$teknisi->set_alamat($this->input->post('alamat'));
				$teknisi->set_kota($this->input->post('kota'));
				$teknisi->set_kodepos($this->input->post('kodepos'));
				$teknisi->set_propinsi($this->input->post('propinsi'));

				$teknisi->set_telepon($this->input->post('telepon'));
				$teknisi->set_email($this->input->post('email'));

				$this->doctrine->em->flush();

				$returned_data = array(
					'status' => 'sukses',
					'username' => $username,
				);

			} else {
				$returned_data = array(
					'status' => 'gagal',
					'error' => validation_errors(),
				);
			}

			echo json_encode($returned_data);

		} else show_404();
	}
	
	function ajax_hapus(){
		
		if(IS_AJAX){
		
			$username = $this->input->post('username');

			$teknisi = $this->doctrine->em->getRepository('models\Teknisi')->findOneBy(array('username' => $username));
			$this->doctrine->em->remove($teknisi);
			$this->doctrine->em->flush();

			$this->session->set_flashdata('status', 'Berhasil menghapus teknisi '.$username);

		} else show_404();
    }
	
	function register(){
	
		$this->config->load('dropdown_options');
		$option_propinsi = $this->config->item('propinsi');
		
		$this->main_content->add_block('Akun')
			->add_username_field('models\Teknisi')
			->add_text_field('nama','Nama');

		$this->main_content->add_block('Keahlian')
			->add_textarea_field('keahlian','Keahlian');

		$this->main_content->add_block('Alamat')
			->add_text_field('alamat','Alamat')
			->add_text_field('kota','Kota')
			->add_text_field('kodepos','Kode Pos')
			->add_dropdown_field('propinsi','Propinsi',$option_propinsi); 

		$this->main_content->add_block('Kontak')
			->add_text_field('telepon','Telepon')
			->add_text_field('email','Email');

		$this->main_content->set_form_register();

		$this->headbar->add_header();
		$this->sidebar->create_custom('p','Username dan password akan otomatis dikirim ke alamat email yang diinputkan');

		$this->load->view('template', array('title' => 'Register Teknisi'));
    }

	function ajax_register(){
		if(IS_AJAX){

			$this->load->library('form_validation');

			if($this->form_validation->run('teknisi') == TRUE){

				$teknisi = new models\Teknisi();

				//generate username & password
				$this->load->library('generator');
				if($this->input->post('tipe_username') == 'manual')
					$username = $this->generator->generate_username('models\Teknisi',$this->input->post('username'));
				else $username = $this->generator->generate_username('models\Teknisi');
				$password = $this->generator->generate_password();

				$teknisi->set_username($username);
				$teknisi->set_password($password);

				//akun
				$teknisi->set_nama($this->input->post('nama'));

				//keahlian
				$teknisi->set_keahlian($this->input->post('keahlian'));

				//alamat
				$teknisi->set_alamat($this->input->post('alamat'));
				$teknisi->set_kodepos($this->input->post('kodepos'));
				$teknisi->set_kota($this->input->post('kota'));
				$teknisi->set_propinsi($this->input->post('propinsi'));

				//kontak
				$teknisi->set_telepon($this->input->post('telepon'));
				$teknisi->set_email($this->input->post('email'));

				//insert ke database
				$this->doctrine->em->persist($teknisi);
				$this->doctrine->em->flush();

				$this->load->library('email_gateway');
				$email = $this->input->post('email');
				$log_email = $this->email_gateway->send_new_akun_email($email, $username, $password);

				$returned_data = array(
					'status' => 'sukses',
				);

				$this->session->set_flashdata('status', 'Berhasil manambah teknisi '.$this->input->post('nama')."! <br /> Username dan password telah terkirim ke email teknisi");
			} else {
				$returned_data = array(
					'status' => 'gagal',
					'error' => validation_errors(),
				);
			}

			echo json_encode($returned_data);

		} else show_404(); 
	}

    function jadwal_kunjungan($username = null, $tahun = null, $bulan = null, $hari = null){
		if(!$username) show_404();

		if(!$tahun) $tahun = date('Y');
		if(!$bulan) $bulan = date('m');
		if(!$hari) $hari = date('d');
		$tanggal_obj = new Datetime("$tahun-$bulan-$hari");
		$kunjungan_id = '';
		$title = 'Jadwal Kunjungan '.$tanggal_obj->format('d M Y');

		// mendapatkan objek Teknisi berdasarkan username
		$teknisi = $this->doctrine->em->getRepository('models\Teknisi')->findOneBy(array('username' => $username));
		if(!$teknisi) show_404();

		$this->headbar->add_header(anchor('teknisi','Teknisi').' > '.$title);

		//seluruh kunjungan sesuai dengan tanggal dan teknisi
		$array_kunjungan= $this->doctrine->em->createQuery("
			SELECT k 
			FROM models\Kunjungan k 
			JOIN k.teknisi t
			WHERE k.tanggal LIKE '$tahun-$bulan-$hari%' 
			AND t.username = '$username'
			")->getResult();

		$this->main_content->add_custom('p','Terdapat '.count($array_kunjungan).' kunjungan yang harus dilaksanakan hari ini');

		if(count($array_kunjungan) > 0){
			$fields = array('customer','alamat','checklists','testimonial');
			$this->creator->create_daftar_kunjungan($array_kunjungan,$fields);
		}
		$this->headbar->add_menu('tambah_kunjungan','+ kunjungan','');

		// menambah block untuk tambah kunjungan
		$this->creator->create_tambah_kunjungan_block($tanggal_obj,$teknisi->get_id());

		$this->main_content->add_script('kunjungan');

		// inisialisasi kalender
		$cal_data = models\Kunjungan::get_calendar_data($tahun,$bulan,'',$username);
		$day_link = base_url()."teknisi/jadwal_kunjungan/$username/";
		$next_prev_url = $this->next_prev_calendar_url.$username;
		$calendar = $this->creator->create_calendar($tahun,$bulan,$next_prev_url,$cal_data);

		$this->sidebar
			->create_identitas($teknisi)
			->create_menu($this->sidebar_menu_links)
			->add_custom('p','Untuk ganti tanggal, klik tanggal dibawah ini')
			->add_calendar($calendar);

		$this->main_content->add_script('checklist_calendar');

		$this->load->view('template', array(
			'title' => $title,
			'kunjungan_id' => $kunjungan_id,
			'day_link' => $day_link,
			'hari' => $hari,
			'bulan' => $bulan,
			'tahun' => $tahun,
		));
	}

	function ajax_calendar($username,$tahun,$bulan){
		if(IS_AJAX){
			$cal_data = models\Kunjungan::get_calendar_data($tahun,$bulan,'',$username);
			$next_prev_url = $this->next_prev_calendar_url.$username;
			$calendar = $this->creator->create_calendar($tahun,$bulan,$next_prev_url,$cal_data);
			echo $calendar;
		} else show_404();
	}

}
?>
